home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Cheat II / original / cheatprefs.c < prev    next >
Text File  |  1994-10-25  |  12KB  |  415 lines

  1. // prefs.c
  2. // handle the prefs dialog 'n stuff
  3.  
  4. #include "cheatprefs.h"
  5. #include "cheatWindow.h"
  6. #include "main.h"
  7. #include "cheat.h"
  8. #include "ResRefs.h"
  9. #include "error.h"
  10.  
  11. // items in dialog
  12.  
  13. #define pOkay 1
  14. #define pCancel 2
  15. #define pTimeUnit 4
  16. #define pTimeVar (pTimeUnit + 2)
  17. #define pTotalUnit 8
  18. #define pTotalVar (pTotalUnit + 2)
  19. #define pInitBal 12
  20. #define pRateTitle 14
  21. #define pRateVar (pRateTitle + 2)
  22. #define pSymbol 18
  23. #define pSymbolLeft 19
  24. #define pSymbolRight 20
  25. #define pRateAdd 22
  26. #define pRateMult 23
  27. #define pDecRate 26
  28. #define pDecTotal 27
  29.  
  30. // the variables related to what we get from prefs dialog
  31.  
  32. Str255 stimeunit = "\pDay";
  33. Str255 stimevar = "\pd";
  34. Str255 stotalunit = "\pBalance";
  35. Str255 stotalvar = "\pb";
  36. Str255 sinitbal = "\pInitial Balance";
  37. Str255 sratetitle = "\pTransaction";
  38. Str255 sratevar = "\pt";
  39. Str255 ssymbol = "\p$";
  40. Str255 DscaleMinS = "\p0", DscaleMaxS = "\p50";
  41. int onleft = true;        // where to place symbol
  42. int ratetype = kadditiverate;
  43. int tdecplaces = 2, bdecplaces = 1;        // digits after decimal point
  44.  
  45. typedef struct {
  46.     short itemType;
  47.     Handle item;
  48.     Rect box;
  49. } iteminfo;
  50.  
  51. #define kNumItems 27
  52.  
  53. void doPrefDialog(void)
  54. {
  55.     DialogPtr dialog;
  56.     Rect newrect;
  57.     DialogTHndl dlogTemplate;
  58.     short hitItem;        // result of ModalDialog
  59.     Rect theBox;
  60.     long ignoreLong;
  61.     Str255 s;
  62.     iteminfo ilist[kNumItems+1];
  63.     int i, sympos, newrate;
  64.     WindowPtr saveport;
  65.     long l;
  66.  
  67.     dialog = GetNewDialog(rPrefDlog, nil, (WindowPtr) -1); verify(dialog);
  68.     for (i=1;i<=kNumItems;i++) {
  69.         GetDItem(dialog, i, &ilist[i].itemType, &ilist[i].item, &ilist[i].box);
  70.         verify(ilist[i].item);
  71.     }
  72.         // now set up the items how we last had them . . .
  73.     SetIText(ilist[pTimeUnit].item, stimeunit);
  74.     SetIText(ilist[pTimeVar].item, stimevar);
  75.     SetIText(ilist[pTotalUnit].item, stotalunit);
  76.     SetIText(ilist[pTotalVar].item, stotalvar);
  77.     SetIText(ilist[pInitBal].item, sinitbal);
  78.     SetIText(ilist[pRateTitle].item, sratetitle);
  79.     SetIText(ilist[pRateVar].item, sratevar);
  80.     SetIText(ilist[pSymbol].item, ssymbol);
  81.     SetCtlValue((ControlHandle) ((onleft == true) ? ilist[pSymbolLeft].item : ilist[pSymbolRight].item), true);
  82.     SetCtlValue((ControlHandle) ((ratetype == kadditiverate) ? 
  83.         ilist[pRateAdd].item : ilist[pRateMult].item), true);
  84.     NumToString(tdecplaces, s); SetIText(ilist[pDecRate].item, s);
  85.     NumToString(bdecplaces, s); SetIText(ilist[pDecTotal].item, s);
  86.     ShowWindow(dialog);
  87.  
  88.         // draw circle around button -- pity this won't update . . .
  89.     GetPort(&saveport);
  90.     SetPort(dialog);
  91.     theBox = ilist[pOkay].box;
  92.     PenSize(3, 3);
  93.     InsetRect(&theBox, -4, -4);
  94.     FrameRoundRect(&theBox, 16, 16);
  95.     PenSize(1, 1);
  96.     SetPort(saveport);
  97.  
  98.         // set some temp vars, don't want to use real things 'cause of cancel button
  99.     sympos = onleft;
  100.     newrate = ratetype;
  101.     do {
  102.         ModalDialog(nil, &hitItem);
  103.         switch (hitItem) {
  104.             case pSymbolLeft: case pSymbolRight:
  105.                 sympos = (hitItem == pSymbolLeft) ? true : false;
  106.                 SetCtlValue((ControlHandle) ilist[pSymbolLeft].item, sympos);
  107.                 SetCtlValue((ControlHandle) ilist[pSymbolRight].item, !sympos);
  108.                 break;
  109.             case pRateAdd: case pRateMult:
  110.                 newrate = (hitItem == pRateAdd) ? kadditiverate : kmultrate;
  111.                 SetCtlValue((ControlHandle) ilist[pRateAdd].item, newrate == kadditiverate);
  112.                 SetCtlValue((ControlHandle) ilist[pRateMult].item, newrate == kmultrate);
  113.                 break;
  114.             default: break;
  115.         }
  116.     } while ((hitItem != pOkay) && (hitItem != pCancel));
  117.     if (hitItem == pOkay) {        // change real values
  118.         GetIText(ilist[pTimeUnit].item, stimeunit);
  119.         GetIText(ilist[pTimeVar].item, stimevar);
  120.         GetIText(ilist[pTotalUnit].item, stotalunit);
  121.         GetIText(ilist[pTotalVar].item, stotalvar);
  122.         GetIText(ilist[pInitBal].item, sinitbal);
  123.         GetIText(ilist[pRateTitle].item, sratetitle);
  124.         GetIText(ilist[pRateVar].item, sratevar);
  125.         GetIText(ilist[pSymbol].item, ssymbol);
  126.         onleft = sympos;
  127.         ratetype = newrate;
  128.         GetIText(ilist[pDecRate].item, s); StringToNum(s, (long *) &l); tdecplaces = l;
  129.         GetIText(ilist[pDecTotal].item, s); StringToNum(s, (long *) &l); bdecplaces = l;
  130.         if (tdecplaces>8) tdecplaces = 8;
  131.         if (bdecplaces>8) bdecplaces = 8;
  132.     }
  133.     DisposDialog(dialog);        // we outta 5000, g
  134. }
  135.  
  136. #undef kNumItems
  137. #undef pOkay
  138. #undef pCancel
  139. #undef pTimeUnit
  140. #undef pTimeVar
  141. #undef pTotalUnit
  142. #undef pTotalVar
  143. #undef pInitBal
  144. #undef pRateTitle
  145. #undef pRateVar
  146. #undef pSymbol
  147. #undef pSymbolLeft
  148. #undef pSymbolRight
  149. #undef pRateAdd
  150. #undef pRateMult
  151.  
  152. // next up is the scale dialog
  153. //
  154. //
  155.  
  156. // items in dialog
  157.  
  158. #define kNumItems 14
  159. #define pOkay 1
  160. #define pCancel 2
  161. #define pBalString 3
  162. #define pTransString 4
  163. #define pMaxTrans 5
  164. #define pMinTrans 6
  165. #define pMaxBalance 8
  166. #define pMinBalance 7
  167. #define pMinDays 9
  168. #define pMaxDays 10
  169. #define pDaysString 11
  170.  
  171. #define SCALE_BASE 255
  172. #define tscaleX 50
  173. #define bscaleX 180
  174. #define dscaleX 270
  175.  
  176. static int filterproc(DialogPtr daDialog, EventRecord *event, int *itemhit)
  177. {
  178.     return false;
  179. }
  180.  
  181. // the variables related to what we get from scale dialog are in main
  182.  
  183. // this code is so ugly it makes me scream.  wow.  I hated writing this crap.
  184. void doScaleDialog(void)
  185. {
  186.     DialogPtr dialog;
  187.     Rect newrect;
  188.     DialogTHndl dlogTemplate;
  189.     short hitItem;        // result of ModalDialog
  190.     Rect theBox;
  191.     long ignoreLong;
  192.     Str255 s, s2;
  193.     iteminfo ilist[kNumItems+1];
  194.     int i, sympos, newrate;
  195.     WindowPtr saveport;
  196.     float min, max; long lmin, lmax;
  197.     struct scale newbal, newtrans;
  198.     Rect rtrans = {SCALE_BASE - 205, tscaleX-5, SCALE_BASE+8, tscaleX+85};
  199.     Rect rbal = {SCALE_BASE - 205, bscaleX-5, SCALE_BASE+8, bscaleX+85};
  200.     Rect rdays = {SCALE_BASE - 205, dscaleX-5, SCALE_BASE+8, dscaleX+68};
  201.     
  202.     newbal = balScale; newtrans = transScale;
  203.     dialog = GetNewDialog(rScaleDlog, nil, (WindowPtr) -1); verify(dialog);
  204.     for (i=1;i<=kNumItems;i++) {
  205.         GetDItem(dialog, i, &ilist[i].itemType, &ilist[i].item, &ilist[i].box);
  206.         verify(ilist[i].item);
  207.     }
  208.         // now set up the items how we last had them . . .
  209.     SetIText(ilist[pDaysString].item, stimeunit);
  210.     SetIText(ilist[pBalString].item, stotalunit);
  211.     SetIText(ilist[pTransString].item, sratetitle);
  212.     
  213.     float2pstr(TscaleMin, s, tdecplaces); SetIText(ilist[pMinTrans].item, s);
  214.     float2pstr(TscaleMax, s, tdecplaces); SetIText(ilist[pMaxTrans].item, s);
  215.     float2pstr(BscaleMin, s, bdecplaces); SetIText(ilist[pMinBalance].item, s);
  216.     float2pstr(BscaleMax, s, bdecplaces); SetIText(ilist[pMaxBalance].item, s);
  217.     NumToString(DscaleMin, s); SetIText(ilist[pMinDays].item, s);
  218.     NumToString(DscaleMax, s2); SetIText(ilist[pMaxDays].item, s2);
  219.     ShowWindow(dialog);
  220.  
  221.         // draw circle around button -- pity this won't update . . .
  222.     GetPort(&saveport);
  223.     SetPort(dialog);
  224.     theBox = ilist[pOkay].box;
  225.     PenSize(3, 3);
  226.     InsetRect(&theBox, -4, -4);
  227.     FrameRoundRect(&theBox, 16, 16);
  228.     PenSize(1, 1);
  229.     DrawScale(&newtrans, tscaleX, SCALE_BASE);
  230.     DrawScale(&newbal, bscaleX, SCALE_BASE);
  231.         TextFont(monaco);
  232.         TextSize(9);
  233.         MoveTo(dscaleX, SCALE_BASE); /* Start at baseline of scale. */
  234.         Line(0,-newbal.totalPixels); /* Draw vertical line for scale. */
  235.         MoveTo(dscaleX + 8, SCALE_BASE + 6);
  236.         DrawString(s);
  237.         MoveTo(dscaleX + 8, SCALE_BASE + 6 - newbal.totalPixels);
  238.         DrawString(s2);
  239.     SetPort(saveport);
  240.  
  241.         // set some temp vars, don't want to use real things 'cause of cancel button
  242.     sympos = onleft;
  243.     newrate = ratetype;
  244.     do {
  245.         ModalDialog(nil, &hitItem);
  246.         GetPort(&saveport);
  247.         SetPort(dialog);
  248.         switch (hitItem) {
  249.             case pMinTrans: case pMaxTrans:
  250.                 GetIText(ilist[pMinTrans].item, s); min = pstr2float(s);
  251.                 GetIText(ilist[pMaxTrans].item, s); max = pstr2float(s);
  252.                 if (min < max) {
  253.                     RecalcScale(&newtrans, min, max);
  254.                     EraseRect(&rtrans);
  255.                     DrawScale(&newtrans, tscaleX, SCALE_BASE);
  256.                 }
  257.                 break;
  258.             case pMinBalance: case pMaxBalance:
  259.                 GetIText(ilist[pMinBalance].item, s); min = pstr2float(s);
  260.                 GetIText(ilist[pMaxBalance].item, s); max = pstr2float(s);
  261.                 if (min < max) {
  262.                     RecalcScale(&newbal, min, max);
  263.                     EraseRect(&rbal);
  264.                     DrawScale(&newbal, bscaleX, SCALE_BASE);
  265.                 }
  266.                 break;
  267.             case pMinDays: case pMaxDays:
  268.                 GetIText(ilist[pMinDays].item, s); StringToNum(s, &lmin);
  269.                 GetIText(ilist[pMaxDays].item, s2); StringToNum(s2, &lmax);
  270.                 if (s[0] > 10) s[0] = 10;
  271.                 if (s2[0] > 10) s2[0] = 10;
  272.                 if ((lmin>=0) && (lmin < lmax)) {
  273.                     TextFont(monaco);
  274.                     TextSize(9);
  275.                     EraseRect(&rdays);
  276.                     MoveTo(dscaleX, SCALE_BASE); /* Start at baseline of scale. */
  277.                     Line(0,-newbal.totalPixels); /* Draw vertical line for scale. */
  278.                     MoveTo(dscaleX + 8, SCALE_BASE + 6);
  279.                     DrawString(s);
  280.                     MoveTo(dscaleX + 8, SCALE_BASE + 6 - newbal.totalPixels);
  281.                     DrawString(s2);
  282.                 }
  283.                 break;
  284.             case pOkay:        // check values, make sure they're okay
  285.                 GetIText(ilist[pMinTrans].item, s); min = pstr2float(s);
  286.                 GetIText(ilist[pMaxTrans].item, s); max = pstr2float(s);
  287.                 if (min >= max) {
  288.                     StopAlert(rRangeAlrt, nil);
  289.                     hitItem = 0;    // they can't leave yet bwahahah
  290.                     break;
  291.                 }
  292.                 GetIText(ilist[pMinBalance].item, s); min = pstr2float(s);
  293.                 GetIText(ilist[pMaxBalance].item, s); max = pstr2float(s);
  294.                 if (min >= max) {
  295.                     StopAlert(rRangeAlrt, nil);
  296.                     hitItem = 0;    // they can't leave yet bwahahah
  297.                     break;
  298.                 }
  299.                 GetIText(ilist[pMinDays].item, s); StringToNum(s, &lmin);
  300.                 GetIText(ilist[pMaxDays].item, s); StringToNum(s, &lmax);
  301.                 if (lmin < 0) {        // no negative days
  302.                     StopAlert(rBackInTimeAlrt, nil);
  303.                     hitItem = 0;    // they can't leave yet bwahahah
  304.                     break;
  305.                 }
  306.                 if (lmin >= lmax) {
  307.                     StopAlert(rRangeAlrt, nil);
  308.                     hitItem = 0;    // they can't leave yet bwahahah
  309.                     break;
  310.                 }
  311.                 break;
  312.             default: break;
  313.         }
  314.         SetPort(saveport);
  315.     } while ((hitItem != pOkay) && (hitItem != pCancel));
  316.     if (hitItem == pOkay) {        // change real values
  317.         GetIText(ilist[pMinTrans].item, s); TscaleMin = pstr2float(s);
  318.         GetIText(ilist[pMaxTrans].item, s); TscaleMax = pstr2float(s);
  319.         GetIText(ilist[pMinBalance].item, s); BscaleMin = pstr2float(s);
  320.         GetIText(ilist[pMaxBalance].item, s); BscaleMax = pstr2float(s);
  321.         GetIText(ilist[pMinDays].item, DscaleMinS); StringToNum(DscaleMinS, &DscaleMin);
  322.         GetIText(ilist[pMaxDays].item, DscaleMaxS); StringToNum(DscaleMaxS, &DscaleMax);
  323.         RecalcScale(&transScale, TscaleMin, TscaleMax);
  324.         RecalcScale(&balScale, BscaleMin, BscaleMax);
  325.     }
  326.     DisposDialog(dialog);        // we outta 5000, g
  327. }
  328.  
  329. // undef items in dialog
  330.  
  331. #undef kNumItems
  332. #undef pOkay
  333. #undef pCancel
  334. #undef pBalString
  335. #undef pTransString
  336. #undef pMaxTrans
  337. #undef pMinTrans
  338. #undef pMaxBalance
  339. #undef pMinBalance
  340. #undef pMinDays
  341. #undef pMaxDays
  342. #undef pDaysString
  343.  
  344.  
  345. // next up is the time dialog
  346. //
  347. //
  348.  
  349. // items in dialog
  350.  
  351. #define kNumItems 7
  352. #define pOkay 1
  353. #define pCancel 2
  354. #define pRegTime 6
  355. #define pRepTime 7
  356.  
  357. // the variables related to what we get from scale dialog are in main
  358.  
  359. long updateTime = 5, replayTime = 5;        // times for the day cycle thing
  360.  
  361. void doTimeDialog(void)            // doing time . . . I'm such the criminal
  362. {
  363.     DialogPtr dialog;
  364.     Rect newrect;
  365.     DialogTHndl dlogTemplate;
  366.     short hitItem;        // result of ModalDialog
  367.     Rect theBox;
  368.     long ignoreLong;
  369.     Str255 s;
  370.     iteminfo ilist[kNumItems+1];
  371.     int i, sympos, newrate;
  372.     WindowPtr saveport;
  373.     long reg, rep;
  374.  
  375.     dialog = GetNewDialog(rTimeDlog, nil, (WindowPtr) -1); verify(dialog);
  376.     for (i=1;i<=kNumItems;i++) {
  377.         GetDItem(dialog, i, &ilist[i].itemType, &ilist[i].item, &ilist[i].box);
  378.         verify(ilist[i].item);
  379.     }
  380.         // now set up the items how we last had them . . .
  381.     NumToString(updateTime, s); SetIText(ilist[pRegTime].item, s);
  382.     NumToString(replayTime, s); SetIText(ilist[pRepTime].item, s);
  383.     ShowWindow(dialog);
  384.  
  385.         // draw circle around button -- pity this won't update . . .
  386.     GetPort(&saveport);
  387.     SetPort(dialog);
  388.     theBox = ilist[pOkay].box;
  389.     PenSize(3, 3);
  390.     InsetRect(&theBox, -4, -4);
  391.     FrameRoundRect(&theBox, 16, 16);
  392.     PenSize(1, 1);
  393.     SetPort(saveport);
  394.  
  395.     do {
  396.         ModalDialog(nil, &hitItem);
  397.         switch (hitItem) {
  398.             case pOkay:        // check values, make sure they're okay
  399.                 GetIText(ilist[pRegTime].item, s); StringToNum(s, ®);
  400.                 GetIText(ilist[pRepTime].item, s); StringToNum(s, &rep);
  401.                 if ((reg < 1) || (rep < 1)) {
  402.                     StopAlert(rBadTimeAlrt, nil);
  403.                     hitItem = 0;    // they can't leave yet bwahahah
  404.                     break;
  405.                 }
  406.             default: break;
  407.         }
  408.     } while ((hitItem != pOkay) && (hitItem != pCancel));
  409.     if (hitItem == pOkay) {        // change real values
  410.         GetIText(ilist[pRegTime].item, s); StringToNum(s, &updateTime);
  411.         GetIText(ilist[pRepTime].item, s); StringToNum(s, &replayTime);
  412.         startTime = 0;        // the arc starts from scratch now
  413.     }
  414.     DisposDialog(dialog);        // we outta 5000, g
  415. }